package cn.itrip.auth.controller;

import cn.itrip.auth.service.TokenService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.ItripTokenVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.ErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

/**
 * Token控制器
 *
 * @author hduser
 */
@Api(tags = "Token控制器")
@Controller
@RequestMapping(value = "/api")
public class TokenController {

    @Resource
    private TokenService tokenService;

    /**
     * 置换token
     *
     * @return 新的token信息
     */
    @ApiOperation(value = "置换token")
    @ApiImplicitParam(value = "token", name = "token", paramType = "header")
    @RequestMapping(value = "/retoken", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody
    Dto replace(HttpServletRequest request) {
        String token = null;
        try {
            token = this.tokenService.reloadToken(request.getHeader("user-agent"), request.getHeader("token"));
            ItripTokenVO vo = new ItripTokenVO(token, Calendar.getInstance().getTimeInMillis() + 2 * 60 * 60 * 1000, Calendar.getInstance().getTimeInMillis());
            return DtoUtil.returnDataSuccess(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
        }

		/*
		 * 请求格式 
		 * $.ajax({
				headers:{
				          Accept:"application/json;charset=utf-8",
				          Content-Type:"application/json;charset=utf-8",
				          token:"lkfajsdlkfjawoier29"// 从cookie中获取
				},
				type："post",
				.....
				})
		 * 
		 */

    }


    @RequestMapping(value = "/validadeToken", method = RequestMethod.GET, produces = "application/json", headers = "token")
    public @ResponseBody
    Dto validate(HttpServletRequest request) {
        try {
            boolean result = tokenService.validate(request.getHeader("user-agent"), request.getHeader("token"));
            if (result) {
                return DtoUtil.returnSuccess("token有效");
            }else {
                return DtoUtil.returnFail("token无效",ErrorCode.AUTH_TOKEN_INVALID);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
        }
    }
}
